<?php

/*
 * 本程序由Qzzm技术团队开发
 * 官方网站：www.qzzm.com
 * 联系QQ：81746336    联系电话：18607775565
 */

namespace Common\DAL;

class DB {

    function __construct() {
        
    }

    /**
     * 通用查询返回单条数据(数组形式)
     * @param 字符 $table=无前缀的表名
     * @param 数组 $data=查询的字段条件组成的数组
     * @return 有数据则返回数组否则返回null
     */
    public function find($table = null, $where = array(), $field = array(), $cache = false) {
        if (isset($table)) {
            try {
                $tablename = ucfirst($table);
                $data = M($tablename)->where($where)->field($field)->cache($cache)->find();
                return $data;
            } catch (Exception $ex) {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * 通用查询返回多条数据(数组形式)
     * @param 字符 $table=无前缀的表名
     * @param 数组 $data=查询的字段条件(数组形式)
     * @param 数组 $order=排序字段(数组形式) array('status'=>'desc','id')
     * @param 数组 $field=要显示的字段(数组形式) array('id','nickname'=>'name')
     * @param 字符 $limit=限制条数 格式"1,10" 或者 "10"
     * @return 有数据则返回数组否则返回null
     */
    public function select($table = null, $where = array(), $order = array(), $field = array(), $limit = null, $cache = false) {

        if (isset($table)) {
            try {
                $tablename = ucfirst($table);
                $list = M($tablename)->where($where)->order($order)->field($field)->limit($limit)->cache($cache)->select();
                return $list;
            } catch (Exception $ex) {
                return null;
            }
        } else {
            return null;
        }
    }

    public function insert($table = null, $data = array()) {
        $result['flag'] = false;
        if (isset($table)) {
            try {
                $tablename = ucfirst($table);
                $model = D($tablename);
                if ($model->create($data)) {
                    $res = $model->add();
                }
                if ($res) {
                    $result['flag'] = true;
                    $result['data'] = $res;
                } else {
                    $result['text'] = $model->getError();
                }
            } catch (Exception $ex) {
                $result['text'] = "DB Error";
            }
        } else {
            $result['text'] = "dt_" . table . ' does not exist';
        }
        return $result;
    }

    public function update($table = null, $where = array(), $data = array()) {
        $result['flag'] = false;

        if (isset($table)) {
            try {
                $tablename = ucfirst($table);
                $model = M($tablename);
                if ($model->create($data)) {
                    $res = $model->where($where)->setField($data);
                }
                if ($res!==false) {
                    $result['flag'] = true;
                    $result['data'] = $res;
                } else {
                    $result['flag'] = true;
                    $result['text'] = $model->getError();
                }
            } catch (Exception $ex) {
                $result['text'] = "DB Error";
            }
        } else {
            $result['text'] = "dt_" . table . ' does not exist';
        }
        
        return $result;
    }

    public function delete($table = null, $where = array()) {
        $result['flag'] = false;
        if (isset($table)) {
            try {
                $tablename = ucfirst($table);
                $res = M($tablename)->where($where)->delete();
                if ($res!==false) {
                    $result['flag'] = true;
                    $result['data'] = $res;
                } else {
                    $result['text'] = $model->getError();
                }
            } catch (Exception $ex) {
                $result['text'] = "DB Error";
            }
        } else {
            $result['text'] = "dt_" . table . ' does not exist';
        }
        return $result;
    }

    public function count($table = null, $where = array(), $feild = 'id', $cache = false) {
        if (isset($table)) {
            try {
                $tablename = ucfirst($table);
                $count = M($tablename)->where($where)->cache($cache)->count($feild);
                return $count;
            } catch (Exception $ex) {
                return null;
            }
        } else {
            return 0;
        }
    }

}
